package blog.controller;

import blog.entity.BatchResult;
import blog.entity.QueryJson;
import blog.entity.ResultJson;
import blog.pojo.Notice;
import blog.pojo.NoticeCategory;
import blog.pojo.Notice;
import blog.service.NoticeCategoryService;
import blog.service.NoticeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/notice")
public class NoticeController {
    
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeCategoryService noticeCategoryService;
    
    @RequestMapping("edit")
    public ResultJson edit(@RequestBody QueryJson<Notice> queryJson) throws Exception{
        if(queryJson.getEntity() == null) {
            return ResultJson.ARGUMENT_NULL;
        }
        Notice notice = queryJson.getEntity();
        Integer noticeId = queryJson.getEntity().getId();
        if(noticeId != null){
            noticeService.update(notice);
        } else {
            noticeService.insert(notice);
        }

        return ResultJson.SUCCESS;
    }

    @RequestMapping("update")
    public ResultJson update(@RequestBody QueryJson<Notice> queryJson) throws Exception{
        if(queryJson.getEntity() == null) {
            return ResultJson.ARGUMENT_NULL;
        }
        noticeService.update(queryJson.getEntity());

        return ResultJson.SUCCESS;
    }

    @RequestMapping("/updateManyByIds")
    public ResultJson updateManyByIds(@RequestBody QueryJson<Notice> queryJson) throws Exception{
        if(queryJson.getEntity() == null || queryJson.getIds() == null){
            return ResultJson.ARGUMENT_NULL;
        }
        int success = 0, fail = 0;
        for(Integer id : queryJson.getIds()){
            Notice notice = queryJson.getEntity().setId(id);
            if (noticeService.update(notice)) {
                success++;
            } else {
                fail++;
            }
        }
        return ResultJson.isSuccess(BatchResult.SET(success,fail));
    }


    @RequestMapping("/findPage")
    public ResultJson findPage(@RequestBody QueryJson<Notice> queryJson) throws Exception{
        if(queryJson.getPage() == null) return ResultJson.ARGUMENT_NULL;

        Page<Notice> page = noticeService.getPage(queryJson.getPage(), queryJson.getEntity(), queryJson.getKeywords());
        for(Notice notice : page.getRecords()){
            NoticeCategory noticeCategory = new NoticeCategory().setId(notice.getCategoryId());
            notice.setCategory(noticeCategoryService.get(noticeCategory));
        }

        return ResultJson.isSuccess(page);
    }

    @RequestMapping("/find")
    public ResultJson find(@RequestBody QueryJson<Notice> queryJson) throws Exception{
        Integer id = queryJson.getEntity().getId();
        if(id == null) return ResultJson.ARGUMENT_NULL;
        Notice notice = noticeService.getInfo(id);
        NoticeCategory noticeCategory = new NoticeCategory().setId(notice.getCategoryId());
        notice.setCategory(noticeCategoryService.get(noticeCategory));
        return ResultJson.isSuccess(notice);
    }
}
